//
//  B_LeaveMessagesVC.m
//  DreamOf
//
//  Created by 薛小刚 on 2018/8/31.
//  Copyright © 2018年 薛小刚. All rights reserved.
//

#import "B_LeaveMessagesVC.h"

@interface B_LeaveMessagesVC ()<UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *B_titleLabel;
@property (weak, nonatomic) IBOutlet UIImageView *B_userImageView;
@property (weak, nonatomic) IBOutlet BaseTextView *B_inputTextView;
@property (weak, nonatomic) IBOutlet UILabel *B_textNumber;
@property (weak, nonatomic) IBOutlet UILabel *B_testAllNumber;
@property (weak, nonatomic) IBOutlet UIButton *B_submitButton;
- (IBAction)B_submitButtonAction:(id)sender;

@end

@implementation B_LeaveMessagesVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"留言"];
    self.B_titleLabel.text = @"\"在这里写下你想和对方说的话\"";
    self.B_userImageView.layer.borderColor = [UIColor whiteColor].CGColor;
    self.B_userImageView.layer.borderWidth = 2;
    self.B_inputTextView.placeholder = @"请输入";
    self.B_textNumber.text = @"0";
    self.B_testAllNumber.text = @"/100";
    self.view.backgroundColor = [UIColor color_content_background_color];
    [self.B_submitButton setBackgroundImage:[UIImage getImageWithColor:[UIColor color_pink] alp:0.3] forState:UIControlStateSelected];
    [self.B_submitButton setBackgroundImage:[UIImage getImageWithColor:[UIColor color_pink] alp:1] forState:UIControlStateNormal];
    self.B_submitButton.selected = YES;
    
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if (textView.text.length > 100) {
        return NO;
    }
    self.B_textNumber.text = [NSString stringWithFormat:@"%ld", textView.text.length];
    return YES;
}

- (void)textViewDidEndEditing:(UITextView *)textView {
    if (textView.text.length > 0) {
        self.B_submitButton.selected = NO;
    } else {
        self.B_submitButton.selected = YES;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)B_submitButtonAction:(id)sender {
    if (self.B_submitButton.isSelected) {
        [KeyWindow makeToast:@"请输入留言"];
    } else {
        
        [NetworkingManager getNetworkRequestWithUrlString:[NSString stringWithFormat:@"/yearnfor/inter/userLeaveWord/addUserLeaveWord/%@/%@/%@", self.B_userId, getUserId(), self.B_inputTextView.text] parameters:nil isCache:NO succeed:^(id data) {
            if ([data[@"code"] isEqualToString:@"20005"]) {
                [KeyWindow makeToast:@"留言成功"];
                [self.navigationController popViewControllerAnimated:YES];
            } else {
                [KeyWindow makeToast:data[@"message"]];
            }
        } fail:^(NSError *error) {
            [KeyWindow makeToast:@"服务器异常"];
        }];
        
        
        //TODO:执行提交操作
    }
}
@end
